package org.example.review.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.review.dto.HrRecordDTO;
import org.example.review.dto.OperationLogDTO;
import org.example.review.dto.UserDTO;
import org.example.review.mapper.HrMapper;
import org.example.review.mapper.OperationLogMapper;
import org.example.review.service.HrService;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@RequiredArgsConstructor
@Service
public class HrServiceImpl implements HrService {
    private final HrMapper hrMapper;

    public List<UserDTO> gethrlist() {
        try {
            log.info("开始查询所有有效用户列表");
            List<UserDTO> userList = hrMapper.selectuserList();
            log.info("成功查询到用户列表，共 {} 条记录", userList.size());
            return userList;
        } catch (Exception e) {
            log.error("查询用户列表时发生数据库异常", e);
            throw new RuntimeException("获取用户数据失败，请稍后再试", e);
        }
    }

    public int insertHrRecord(HrRecordDTO hrRecord){
        return hrMapper.insertHrRecord(hrRecord);
    }
    public int updateHr(UserDTO userDTO){
        return hrMapper.updateHr(userDTO);
    }
    public int deleteHr(UserDTO userDTO){
        return hrMapper.deleteHr(userDTO);
    }
    public int addHr(UserDTO userDTO){
        return hrMapper.addHr(userDTO);
    }
    
    /**
     * 批量删除用户
     * @param userIds 用户ID列表
     * @return 删除的记录数
     */
    @Override
    public int batchDeleteHr(List<Integer> userIds) {
        try {
            log.info("开始批量删除用户，ID列表: {}", userIds);
            if (userIds == null || userIds.isEmpty()) {
                log.warn("批量删除失败：用户ID列表为空");
                throw new IllegalArgumentException("用户ID列表不能为空");
            }
            int deletedCount = hrMapper.batchDeleteHr(userIds);
            log.info("成功批量删除 {} 个用户", deletedCount);
            return deletedCount;
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (Exception e) {
            log.error("批量删除用户时发生数据库异常", e);
            throw new RuntimeException("批量删除用户失败，请稍后再试", e);
        }
    }
    
    /**
     * 批量更新用户信息
     * @param userIds 用户ID列表
     * @param userDTO 包含要更新的字段
     * @return 更新后的用户列表
     */
    @Override
    public List<UserDTO> batchUpdateHr(List<Integer> userIds, UserDTO userDTO) {
        try {
            log.info("开始批量更新用户，ID列表: {}, 更新数据: {}", userIds, userDTO);
            if (userIds == null || userIds.isEmpty()) {
                log.warn("批量更新失败：用户ID列表为空");
                throw new IllegalArgumentException("用户ID列表不能为空");
            }
            int updatedCount = hrMapper.batchUpdateHr(userIds, userDTO);
            log.info("成功批量更新 {} 个用户", updatedCount);
            
            // 查询并返回更新后的用户列表
            List<UserDTO> allUsers = hrMapper.selectuserList();
            List<UserDTO> updatedUsers = allUsers.stream()
                    .filter(user -> userIds.contains(user.getUserId()))
                    .collect(java.util.stream.Collectors.toList());
            
            log.info("返回更新后的用户列表，共 {} 条记录", updatedUsers.size());
            return updatedUsers;
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (Exception e) {
            log.error("批量更新用户时发生数据库异常", e);
            throw new RuntimeException("批量更新用户失败，请稍后再试", e);
        }
    }
    
    /**
     * 获取所有人事操作记录
     * @return 操作记录列表
     */
    @Override
    public List<HrRecordDTO> getHrRecordList() {
        try {
            log.info("开始查询人事操作记录列表");
            List<HrRecordDTO> recordList = hrMapper.selectHrRecordList();
            log.info("成功查询到操作记录列表，共 {} 条记录", recordList.size());
            return recordList;
        } catch (Exception e) {
            log.error("查询操作记录列表时发生数据库异常", e);
            throw new RuntimeException("获取操作记录失败，请稍后再试", e);
        }
    }
    
    /**
     * 删除单条操作记录
     * @param recordId 记录ID
     * @return 删除的记录数
     */
    @Override
    public int deleteHrRecordById(Integer recordId) {
        try {
            log.info("开始删除操作记录，记录ID: {}", recordId);
            if (recordId == null) {
                log.warn("删除失败：记录ID为空");
                throw new IllegalArgumentException("记录ID不能为空");
            }
            int deletedCount = hrMapper.deleteHrRecord(recordId);
            log.info("成功删除操作记录，记录ID: {}", recordId);
            return deletedCount;
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (Exception e) {
            log.error("删除操作记录时发生数据库异常", e);
            throw new RuntimeException("删除操作记录失败，请稍后再试", e);
        }
    }
    
    /**
     * 批量删除操作记录
     * @param recordIds 记录ID列表
     * @return 删除的记录数
     */
    @Override
    public int batchDeleteHrRecords(List<Integer> recordIds) {
        try {
            log.info("开始批量删除操作记录，ID列表: {}", recordIds);
            if (recordIds == null || recordIds.isEmpty()) {
                log.warn("批量删除失败：记录ID列表为空");
                throw new IllegalArgumentException("记录ID列表不能为空");
            }
            int deletedCount = hrMapper.batchDeleteHrRecords(recordIds);
            log.info("成功批量删除 {} 条操作记录", deletedCount);
            return deletedCount;
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (Exception e) {
            log.error("批量删除操作记录时发生数据库异常", e);
            throw new RuntimeException("批量删除操作记录失败，请稍后再试", e);
        }
    }
    
    /**
     * 更新操作记录的备注
     * @param recordId 记录ID
     * @param remark 新备注
     * @return 更新的记录数
     */
    @Override
    public int updateHrRecordRemark(Integer recordId, String remark) {
        try {
            log.info("开始更新操作记录备注，记录ID: {}, 新备注: {}", recordId, remark);
            if (recordId == null) {
                log.warn("更新失败：记录ID为空");
                throw new IllegalArgumentException("记录ID不能为空");
            }
            int updatedCount = hrMapper.updateHrRecordRemark(recordId, remark);
            log.info("成功更新操作记录备注，记录ID: {}", recordId);
            return updatedCount;
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (Exception e) {
            log.error("更新操作记录备注时发生数据库异常", e);
            throw new RuntimeException("更新操作记录备注失败，请稍后再试", e);
        }
    }
}
